package com.jgzx.json;

import cn.hutool.core.convert.Convert;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializerBase;

import java.io.IOException;

/**
 * 自定义fastJson序列化，将集合里面的数据转换成字符串
 * @author libo
 * @date 2021/3/9
 */
public class ArrayToStringSerialize extends ToStringSerializerBase {


    // 使用单例实现
    public final static ArrayToStringSerialize instance = new ArrayToStringSerialize();

    public ArrayToStringSerialize() {
        super(Object.class);
    }

    public ArrayToStringSerialize(Class<?> handledType) {
        super(handledType);
    }

    @Override
    public void serialize(Object values, JsonGenerator gen, SerializerProvider provider) throws IOException {
        String[] strings = Convert.toStrArray(values);
        gen.writeArray(strings,0,strings.length);
    }

    @Override
    public String valueToString(Object value) {
        return value.toString();
    }
}
